package com.qf.controller;/*
 *徐韬
 *2021/10/26
 *19:48
 */

import com.github.pagehelper.PageInfo;
import com.qf.dto.R;
import com.qf.dto.UserDto;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.TAdminUser;
import com.qf.pojo.TBlackList;
import com.qf.service.TBlacklistService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/black")
@Api(tags = "黑名单管理")
public class TBlackController {
    private TBlacklistService tBlacklistService;
    @Autowired
    public void settBlacklistService(TBlacklistService tBlacklistService) {
        this.tBlacklistService = tBlacklistService;
    }
    @PostMapping("/add/{mobile}")
    public R add(@PathVariable String mobile, HttpSession session){
        System.out.println(mobile);
        TBlackList tBlackList = new TBlackList();
        tBlackList.setMobile(mobile);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        if(myAuthUser!= null){
            Long type = myAuthUser.getType();
            tBlackList.setOwntype(type);
            Long id = myAuthUser.getId();
            tBlackList.setCreater(id);
        }
        System.out.println(tBlackList);
         tBlacklistService.addblack(tBlackList);
        return R.setOk();


    }
    @GetMapping("/showAll")
    public R showAll(@RequestParam(defaultValue = "4") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        System.out.println("111");
        System.out.println(pageNum);
        System.out.println(pageSize);
        PageInfo<TBlackList> pageInfo = tBlacklistService.showAll(pageNum, pageSize);
        return R.setOk(pageInfo);
    }
    @PostMapping("/update")
    public R updateBlack(@RequestBody TBlackList tBlackList,HttpSession session){
        TAdminUser user = (TAdminUser) session.getAttribute("user");
        if(user!=null){
            Long id = user.getId();
            Long type = user.getType();
            tBlackList.setCreater(id);
            tBlackList.setOwntype(type);
        }
        tBlacklistService.updateblack(tBlackList);
        return R.setOk();
    }
    @DeleteMapping("/delete")
    public R deleteByids(@RequestBody Map map){
        System.out.println(map);
        List<Integer> ids =(List<Integer>) map.get("ids");
        tBlacklistService.deleteBlackByIds(ids);
        return R.setOk();
    }
    @GetMapping("/select/{id}")
    public R select(@PathVariable Long id){
        TBlackList list = tBlacklistService.selectById(id);
        return R.setOk(list);
    }
    @GetMapping("/showAllBypramas")
    public R showAllBypramas(TBlackList tBlackList){
        List<TBlackList> tBlackListList = tBlacklistService.showAllByparams(tBlackList);
        return R.setOk(tBlackListList);
    }
}
